﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cinch;
using System.ComponentModel;
using System.ComponentModel.Composition;
using MEFedMVVM.ViewModelLocator;
using System.Windows.Data;

namespace Anata.Client
{
    /// <summary>
    /// Represents a single Image ViewModel
    /// </summary>
    public class LayananViewModel : EditableValidatingViewModelBase
    {
        #region Data
        private ViewMode currentViewMode = ViewMode.AddMode;

        #region Data.Primitives
        private DataWrapper<Int32> idLayanan;
        private DataWrapper<String> nama;
        private DataWrapper<Int32> durasi;
        #endregion
        private IEnumerable<DataWrapperBase> cachedListOfDataWrappers;
        #region Data.Rules
        #endregion

        #region Data.VMs
        #endregion

        #region Data.Services
        #endregion
        #endregion

        #region constructor
        [ImportingConstructor]
        public LayananViewModel()
        {
            //setup services
            #region Setup Services
            #endregion

            #region Setup Commands
            #endregion

            #region Create DataWrappers
            IdLayanan = new DataWrapper<Int32>(this, idLayananChangeArgs);
            IdLayanan.IsEditable = true;
            Nama = new DataWrapper<String>(this, namaChangeArgs);
            Nama.IsEditable = true;
            Durasi = new DataWrapper<Int32>(this, durasiChangeArgs);
            Durasi.IsEditable = true;
            //fetch list of all DataWrappers, so they can be used again later without the
            //need for reflection
            cachedListOfDataWrappers =
                DataWrapperHelper.GetWrapperProperties<LayananViewModel>(this);
            #endregion

            #region Create Validation Rules
            #endregion
        }

        static LayananViewModel()
        {

        }
        #endregion

        #region Public Properties
        //commands
        #region Public.Commands
        #endregion

        #region Public.ViewMode
        /// <summary>
        /// The current ViewMode, when changed will loop
        /// through all nested DataWrapper objects and change
        /// their state also
        /// </summary>

        static PropertyChangedEventArgs currentViewModeChangeArgs =
            ObservableHelper.CreateArgs<LayananViewModel>(x => x.CurrentViewMode);

        public ViewMode CurrentViewMode
        {
            get { return currentViewMode; }
            set
            {
                currentViewMode = value;

                switch (currentViewMode)
                {
                    case ViewMode.AddMode:
                        //Quantity.DataValue= 0;
                        this.DisplayName = "Transaksi Baru";
                        break;
                    case ViewMode.EditMode:
                        this.DisplayName = "Ubah Transaksi";
                        BeginEdit();
                        break;
                    case ViewMode.ViewOnlyMode:
                        this.DisplayName = "Lihat Transaksi";
                        break;
                }

                //Now change all the CachedListOfDataWrappers
                //Which sets all the Cinch.DataWrapper<T>s to the correct IsEditable
                //state based on the new ViewMode applied to the ViewModel
                //we can use the Cinch.DataWrapperHelper class for this
                DataWrapperHelper.SetMode(
                    CachedListOfDataWrappers,
                    currentViewMode);

                NotifyPropertyChanged(currentViewModeChangeArgs);
            }
        }
        #endregion

        #region Public.Data
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs idLayananChangeArgs =
            ObservableHelper.CreateArgs<LayananViewModel>(x => x.IdLayanan);
        public DataWrapper<Int32> IdLayanan
        {
            get { return idLayanan; }
            set
            {
                idLayanan = value;
                NotifyPropertyChanged(idLayananChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs namaChangeArgs =
            ObservableHelper.CreateArgs<LayananViewModel>(x => x.Nama);
        public DataWrapper<String> Nama
        {
            get { return nama; }
            set
            {
                nama = value;
                NotifyPropertyChanged(namaChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs durasiChangeArgs =
            ObservableHelper.CreateArgs<LayananViewModel>(x => x.Durasi);
        public DataWrapper<Int32> Durasi
        {
            get { return durasi; }
            set
            {
                durasi = value;
                NotifyPropertyChanged(durasiChangeArgs);
            }
        }
        #endregion

        #region Public Cached Collection
        /// <summary>
        /// Returns cached collection of DataWrapperBase
        /// </summary>
        public IEnumerable<DataWrapperBase> CachedListOfDataWrappers
        {
            get { return cachedListOfDataWrappers; }
        }
        #endregion

        #region Public.DataVM
        #endregion

        #endregion

        #region Command Handlers
        #endregion

        #region Private Methods
        #endregion

        #region Overrides

        static PropertyChangedEventArgs isValidChangeArgs =
            ObservableHelper.CreateArgs<LayananViewModel>(x => x.IsValid);

        public override bool IsValid
        {
            get
            {
                //return base.IsValid and use DataWrapperHelper, if you are
                //using DataWrappers
                return base.IsValid &&
                    DataWrapperHelper.AllValid(cachedListOfDataWrappers);
            }

        }

        public override string ToString()
        {
            return String.Format("LayananViewModel {0}", this.idLayanan);
        }
        #endregion

        #region EditableValidatingViewModelBase overrides
        protected override void OnBeginEdit()
        {
            base.OnBeginEdit();
            //Now walk the list of properties in the OrderViewModel
            //and call BeginEdit() on all Cinch.DataWrapper<T>s.
            //we can use the Cinch.DataWrapperHelper class for this
            DataWrapperHelper.SetBeginEdit(cachedListOfDataWrappers);
        }

        /// <summary>
        /// Override hook which allows us to also put any child 
        /// EditableValidatingViewModelBase objects into the EndEdit state
        /// </summary>
        protected override void OnEndEdit()
        {
            base.OnEndEdit();
            //Now walk the list of properties in the OrderViewModel
            //and call CancelEdit() on all Cinch.DataWrapper<T>s.
            //we can use the Cinch.DataWrapperHelper class for this
            DataWrapperHelper.SetEndEdit(cachedListOfDataWrappers);
        }

        /// <summary>
        /// Override hook which allows us to also put any child 
        /// EditableValidatingViewModelBase objects into the CancelEdit state
        /// </summary>
        protected override void OnCancelEdit()
        {
            base.OnCancelEdit();
            //Now walk the list of properties in the OrderViewModel
            //and call CancelEdit() on all Cinch.DataWrapper<T>s.
            //we can use the Cinch.DataWrapperHelper class for this
            DataWrapperHelper.SetCancelEdit(cachedListOfDataWrappers);
        }
        #endregion
    }
}
